package game.units;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;

import utils.DBG;
import utils.GameMaths;
import core.pathfinder.Pathfinder;
import core.pathfinder.Route;
import core.pathfinder.RoutePoint;

public class SimpleUnit extends Unit {

	RoutePoint objective;
	float velocity = 2;
	float min_distance = 5;
	double angle_objective;
	public boolean needNewObjective = false;
	
	Route route;

	public SimpleUnit() {
		super(0, 0);
		live = 200;
		money = 2;
	}

	public SimpleUnit(int xpos, int ypos, int add) {
		super(xpos, ypos);
		live = 1000;
		money = 2 + 2 * add;
		DBG.showMsg("Live : " + live, this);
	}
	Point begin;
	Point end;
	
	public SimpleUnit (Point begin_, Point end_){
		super(begin_.x *32, begin_.y *32);
		begin = begin_;
		end = end_;
		live = 100;
		money = 2;
	}

	public SimpleUnit(int xpos, int ypos) {
		super(xpos, ypos);
	}

	@Override
	public Unit clone() {
		// TODO Auto-generated method stub
		SimpleUnit u = new SimpleUnit();
		u.live = live;
		u.velocity = velocity;
		return u;
	}

	@Override
	public void move(float x, float y) {
		// TODO Auto-generated method stub
		this.setX(x);
		this.setY(y);
	}

	@Override
	public void substract(int live_lost) {
		// TODO Auto-generated method stub
		live -= live_lost;
	}

	@Override
	public void renderComponent(Graphics2D g2d) {
		// TODO Auto-generated method stub
		g2d.setColor(Color.gray);
		g2d.fillOval((int) getX() - 6, (int) getY() - 6, 12, 12);
		if (route != null){
			route.render(g2d);
		}
	}

	@Override
	public void updateLogic() {
		// TODO Auto-generated method stub
		if (this.objective != null) {
			if (!inObjective()) {
				this.moveToObtective();
			} else {
				this.needNewObjective = true;
			}
		}
	}
	
	public void recalculateRoute(){
		route = null;
		route = Pathfinder.getInstance().getRoute((int)this.getX()/32, (int)this.getY()/32, end.x, end.y);
		objective = route.getBegin();
	}
	
	public void setRoute(Route newRoute){
		route = newRoute;
	}
	
	public Route getRoute(){
		return route;
	}

	public void setObjective(RoutePoint newObjective) {
		objective = newObjective;
		needNewObjective = false;
	}

	public RoutePoint getObjective() {
		return objective;
	}

	public boolean inObjective() {
		return GameMaths.distance(this.getX(), this.getY(),
				objective.getXpos(), objective.getYpos()) < min_distance;
	}

	@Override
	public int getNavigationType() {
		// TODO Auto-generated method stub
		return Unit.NAVIGATION_GROUND;
	}

	public double getVelocity() {
		return velocity;
	}

	public void moveToObtective() {
		/**
		 * dirX = destX - posX; dirY = destY - posY; normalizas velX = dirX *
		 * speed newX = posX + velX Rob dice: y lo mismo para Y
		 * 
		 */
		if (objective != null) {
			float dirx = objective.getXpos() - this.getX();
			float diry = objective.getYpos() - this.getY();

			float module = (float) Math.sqrt((dirx * dirx) + (diry * diry));

			dirx /= module;
			diry /= module;

			// DBG.showMsg("Module: " + module + " dirx : " + dirx + " diry : "
			// + diry, this);

			float incrX = dirx * velocity;
			float nexX = this.getX() + incrX;

			float incrY = diry * velocity;
			float newY = this.getY() + incrY;

			this.move(nexX, newY);
		}
	}

	@Override
	public int getMoney() {
		// TODO Auto-generated method stub
		return 2;
	}
	
	public Point getPosition(){
		return new Point((int)this.getX() , (int)this.getY());
	}

}
